package com.example.hyz.xrecylerviewtt;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.example.hyz.xrecylerviewtt.xrecylerviewbase.CommonAdapter;
import com.example.hyz.xrecylerviewtt.xrecylerviewbase.CommonViewHolder;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    public static final int FLAG_INIT_DATA = 0;
    public static final int FLAG_REFRESH_DATA = 1;
    public static final int FLAG_LOADMORE_DATA = 2;

    private ArrayList<StudentBeans> lists = new ArrayList<>();
    ArrayList<StudentBeans> remoteLists = new ArrayList<>();
    private int curPage = 0;

    private XRecyclerView xRecyclerView;

    CommonAdapter<StudentBeans> adapter;
    @SuppressLint("HandlerLeak")
    private Handler dataHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case FLAG_INIT_DATA:
                    lists.clear();
                    lists.addAll(remoteLists);
                    adapter.notifyDataSetChanged();
                    break;

                case FLAG_REFRESH_DATA:
                    lists.clear();
                    lists.addAll(remoteLists);
                    xRecyclerView.refreshComplete();
                    adapter.notifyDataSetChanged();
                    break;

                case FLAG_LOADMORE_DATA:
                    lists.addAll(remoteLists);
                    xRecyclerView.loadMoreComplete();
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xRecyclerView = (XRecyclerView)findViewById(R.id.rcv);
        initRecyler();
        initData();
    }

    private void initRecyler() {
        //LinearLayoutManager是线性布局，setOrientation可以设置他的方向是横向还是纵向。
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }

            @Override
            public void onLoadMore() {
                addData();
            }
        });

        adapter = new CommonAdapter<StudentBeans>(this , R.layout.item_students , lists) {
            @Override
            public void convert(CommonViewHolder holder, StudentBeans studentBeans, int position) {
                holder.setText(R.id.itm_tv_name , studentBeans.getName())
                        .setText(R.id.itm_tv_age , studentBeans.getAge());
            }
        };
        xRecyclerView.setAdapter(adapter);
        View headView = LayoutInflater.from(this).inflate(R.layout.header , null , false);
        View footView = LayoutInflater.from(this).inflate(R.layout.footer , null , false);
//        xRecyclerView.addHeaderView(headView);
//        xRecyclerView.setFootView(footView , null);
        xRecyclerView.getDefaultRefreshHeaderView() // get default refresh header view
                .setRefreshTimeVisible(true);  // make refresh time visible,false means hiding
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                remoteLists.clear();
                for (int i = 0; i < 15; i++) {
                    StudentBeans studentBeans = new StudentBeans("第" + curPage + "批" +"小妹妹第" + i + "号", "今年" + (16 + i) + "岁" ,"暂时没得图片");
                    remoteLists.add(studentBeans);
                }
                Message msg = dataHandler.obtainMessage(FLAG_INIT_DATA);
                dataHandler.sendMessage(msg);
            }
        }).start();

    }
    
    

    private void addData() {
        curPage+=1;
        new Thread(new Runnable() {
            @Override
            public void run() {
                remoteLists.clear();
                for (int i = 0; i < 15; i++) {
                    StudentBeans studentBeans = new StudentBeans("第" + curPage + "批" +"小妹妹第" + i + "号", "今年" + (26 + i) + "岁" ,"暂时没得图片");
                    remoteLists.add(studentBeans);
                }
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = dataHandler.obtainMessage(FLAG_LOADMORE_DATA);
                dataHandler.sendMessage(msg);
            }
        }).start();
    }

    private void refreshData() {
        curPage = 0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                remoteLists.clear();
                for (int i = 0; i < 15; i++) {
                    StudentBeans studentBeans = new StudentBeans("老妹儿" + i + "号", "今年" + (26 + i) + "岁" ,"暂时没得图片");
                    remoteLists.add(studentBeans);
                }
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = dataHandler.obtainMessage(FLAG_REFRESH_DATA);
                dataHandler.sendMessage(msg);
            }
        }).start();
    }


}
